package com.hgyc.mom.wms.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.common.enums.CommonStatus;
import com.hgyc.mom.wms.service.WmsWarehouseUserService;
import com.hgyc.mom.wms.vo.WmsWarehouseVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.WmsWarehouse;
import com.hgyc.mom.wms.vo.WmsWarehouseParam;
import com.hgyc.mom.wms.service.WmsWarehouseService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 仓库Controller
 *
 * @author fwj
 * @since 2025-05-14
*/
@Tag(name = "仓库控制器", description = "仓库控制器")
@RestController
@RequestMapping("/wms/warehouse")
public class WmsWarehouseController {
    @Autowired
    private WmsWarehouseService wmsWarehouseService;

    @Autowired
    private WmsWarehouseUserService wmsWarehouseUserService;

    @Operation(summary = "查询仓库列表", description = "查询仓库列表")
    @PostMapping("/pageList")
    public Result<IPage<WmsWarehouse>> pageList(@Parameter(description = "查询参数") @RequestBody WmsWarehouseParam wmsWarehouseParam) {
        Page<WmsWarehouse> page = new Page<>(wmsWarehouseParam.getPage(), wmsWarehouseParam.getPageSize());
        WmsWarehouse wmsWarehouse = new WmsWarehouse();
        BeanUtils.copyProperties(wmsWarehouseParam, wmsWarehouse);
        QueryWrapper<WmsWarehouse> wrapper = QueryWrapperUtil.build(wmsWarehouse);

        if (wmsWarehouse.getParentId() != null) {
            wrapper.eq("parent_id", wmsWarehouse.getParentId()).or().eq("id", wmsWarehouse.getParentId());
        }

        IPage<WmsWarehouse> list = wmsWarehouseService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询仓库树列表", description = "查询仓库树列表")
    @PostMapping("/treeList")
    public Result<List<WmsWarehouse>> treeList(@Parameter(description = "查询参数") @RequestBody WmsWarehouseParam wmsWarehouseParam) {
        List<WmsWarehouse> list = wmsWarehouseService.treeList(wmsWarehouseParam);
        return Result.success(list);
    }

    @Operation(summary = "仓库下拉列表", description = "仓库下拉列表")
    @GetMapping("/select/list")
    public Result<List<WmsWarehouse>> warehouseOptions() {
        LambdaQueryWrapper<WmsWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmsWarehouse::getParentId, 0);
        wrapper.eq(WmsWarehouse::getEnableFlag, CommonStatus.ENABLE.getValue());
        return Result.success(wmsWarehouseService.list(wrapper));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsWarehouse> util = new ExcelUtil<>(WmsWarehouse.class);
        util.importTemplateExcel(response, "仓库");
    }

    @Operation(summary = "导出仓库列表", description = "导出仓库列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsWarehouseParam wmsWarehouseParam) {
    WmsWarehouse wmsWarehouse = new WmsWarehouse();
        BeanUtils.copyProperties(wmsWarehouseParam, wmsWarehouse);
        QueryWrapper<WmsWarehouse> wrapper = QueryWrapperUtil.build(wmsWarehouse);
        List<WmsWarehouse> list = wmsWarehouseService.list(wrapper);
        ExcelUtil<WmsWarehouse> util = new ExcelUtil<>(WmsWarehouse.class);
        util.exportExcel(response, list, "仓库数据");
    }

    @Operation(summary = "导入仓库列表", description = "导入仓库列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsWarehouse> util = new ExcelUtil<>(WmsWarehouse.class);
        List<WmsWarehouse> wmsWarehouseList = util.importExcel(file.getInputStream());
        String message = wmsWarehouseService.importData(wmsWarehouseList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取仓库详细信息", description = "获取仓库详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "仓库ID") @PathVariable("id") Long id) {
        return Result.success(wmsWarehouseService.getById(id));
    }

    @Operation(summary = "新增仓库", description = "新增仓库")
    @PostMapping
    public Result save(@Parameter(description = "仓库") @RequestBody WmsWarehouseVO wmsWarehouseVO) {
        wmsWarehouseService.saveWarehouse(wmsWarehouseVO);
        return Result.success();
    }

    @Operation(summary = "修改仓库", description = "修改仓库")
    @PutMapping
    public Result update(@Parameter(description = "仓库") @RequestBody WmsWarehouseVO wmsWarehouseVO) {
        wmsWarehouseService.updateWarehouse(wmsWarehouseVO);
        return Result.success();
    }

    @Operation(summary = "删除仓库", description = "删除仓库")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(wmsWarehouseService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "获取仓库人员", description = "获取仓库人员")
    @GetMapping("/users/{warehouseId}")
    public Result warehouseUsers(@PathVariable("warehouseId") Long warehouseId) {
        return Result.success(wmsWarehouseUserService.getWarehouseUsers(warehouseId));
    }
}